﻿using System.Collections.Generic;
using System.IO;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;
using Castle.ActiveRecord.Framework.Internal;
using log4net.Config;
using NHibernate.ByteCode.Castle;
using NHibernate.Cfg;
using NHibernate.Connection;
using NHibernate.Dialect;
using NHibernate.Driver;
using NUnit.Framework;

namespace LazyTests {
    [TestFixture]
    public class Interceptors {
        [TestFixtureSetUp]
        public void FixtureSetup() {
            BasicConfigurator.Configure();
            var arConfig = new InPlaceConfigurationSource();
            var properties = new Dictionary<string, string> {
                {Environment.ConnectionDriver, typeof (SQLite20Driver).FullName},
                {Environment.Dialect, typeof (SQLiteDialect).FullName},
                {Environment.ConnectionProvider, typeof (DriverConnectionProvider).FullName},
                {Environment.ConnectionString, "Data Source=test.db;Version=3;New=True;"},
                {Environment.ProxyFactoryFactoryClass, typeof (ProxyFactoryFactory).AssemblyQualifiedName},
            };

            arConfig.Add(typeof (ActiveRecordBase), properties);
            ActiveRecordStarter.ResetInitializationFlag();
            ActiveRecordStarter.Initialize(arConfig, typeof (User));
            ActiveRecordStarter.CreateSchema();
        }

        [ActiveRecord]
        public class User: ActiveRecordBase<User> {
            [PrimaryKey]
            public virtual int Id { get; set; }

            [Property]
            public virtual string Name { get; set; }

            public virtual int OnSaveCalled { get; private set; }

            protected override void OnSave() {
                OnSaveCalled++;
            }

            public virtual int OnUpdateCalled { get; private set; }

            protected override void OnUpdate() {
                OnUpdateCalled++;
            }
        }

        [Test]
        public void CreateCallsOnSave() {
            var u = new User();
            u.Create();
            Assert.AreEqual(1, u.OnSaveCalled);
        }

        [Test]
        public void UpdateCallsOnSave() {
            var u = new User();
            u.Create();
            Assert.AreEqual(1, u.OnSaveCalled);
            u.Name = "pepe";
            u.Update();
            Assert.AreEqual(1, u.OnSaveCalled);
            Assert.AreEqual(1, u.OnUpdateCalled);
        }

        [Test]
        public void CreateSchemaFromFile() {
            string filename = Path.GetTempFileName();
            try {
                File.WriteAllText(filename, "create table pepe (id int primary key, name varchar);");
                var lines = ARSchemaCreator.OpenFileAndStripContents(filename);
                Assert.Greater(lines.Length, 0);
                ActiveRecordStarter.CreateSchemaFromFile(filename);
                var connection = ActiveRecordMediator.GetSessionFactoryHolder().CreateSession(typeof (object)).Connection;
                using (var cmd = connection.CreateCommand()) {
                    cmd.CommandText = "select * from pepe";
                    using (var reader = cmd.ExecuteReader())
                        reader.Read();
                }
            } finally {
                File.Delete(filename);
            }
        }
    }
}